package com.qlylkj.lbk.model.repository

import com.qlylkj.lbk.app.PreferenceHelper
import com.qlylkj.lbk.model.api.BaseRepository
import com.qlylkj.lbk.model.api.ResultResponse
import com.qlylkj.lbk.model.api.RetrofitHelper
import com.qlylkj.lbk.model.api.UserApi
import com.qlylkj.lbk.model.bean.User
import com.qlylkj.lbk.model.bean.UserInfo
import okhttp3.RequestBody

class UserRepository: BaseRepository(){

    private val userApi by lazy { RetrofitHelper.getService(UserApi::class.java) }

    private fun requestBodyLogin(username: String,password: String): RequestBody {
        val map = mutableMapOf<String, Any>()
        map["username"] = username
        map["password"] = password
        return requestJsonBody(map)
    }

    private fun requestBodyRegister(phoneNumber: String,password: String,email: String = ""): RequestBody {
        val map = mutableMapOf<String, Any>()
        map["username"] = phoneNumber
        map["password"] = password
        map["cphone"] = phoneNumber
        if(email.isNotEmpty()){
            map["email"] = email
        }
        return requestJsonBody(map)
    }

    private fun requestBodyGetUserInfo(): MutableMap<String,Any> {
        val map = mutableMapOf<String, Any>()
        map["id"] = PreferenceHelper.userId
        return map
    }

    private fun requestBodyRemoveBind(): MutableMap<String,Any>{
        val map = mutableMapOf<String, Any>()
        map["userid"] = PreferenceHelper.userId
        return map
    }

    suspend fun register(phoneNumber: String,password: String): ResultResponse<Any> {
        return safeApiCallEntity(
            call = { requestRegister(phoneNumber, password) },
            errorMessage = "注册失败"
        )
    }

    private suspend fun requestRegister(phoneNumber: String,password: String): ResultResponse<Any>{
        val response = userApi.register(requestBodyRegister(phoneNumber, password))
        return executeEntityResponse(response, {

        })
    }

    suspend fun login(username: String,password: String): ResultResponse<User> {
        return safeApiCallEntity(
            call = { requestLogin(username, password) },
            errorMessage = "登录失败"
        )
    }

    private suspend fun requestLogin(username: String,password: String): ResultResponse<User>{
        val response = userApi.login(requestBodyLogin(username, password))
        return executeEntityResponse(response, {

        })
    }

    suspend fun getUserInfo(): ResultResponse<UserInfo>{
        return safeApiCallEntity(
            call = { requestGetUserInfo() },
            errorMessage = "获取用户信息失败"
        )
    }

    private suspend fun requestGetUserInfo(): ResultResponse<UserInfo>{
        val response = userApi.getUserInfo(requestBodyGetUserInfo())
        return executeEntityResponse(response, {

        })
    }

    suspend fun removeBind(): ResultResponse<Any>{
        return safeApiCallEntity(
            call = { requestRemoveBind() },
            errorMessage = "登录失败"
        )
    }

    private suspend fun requestRemoveBind(): ResultResponse<Any>{
        val response = userApi.removeBind(requestBodyRemoveBind())
        return executeEntityResponse(response, {

        })
    }

}